package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.JsonUtil;
import com.coolbi.onsupai.core.biz.CreditBiz;
import com.coolbi.onsupai.core.entity.Credit;
import com.coolbi.security.web.action.SessionAction;

/**
 *@description:会员积分等级说明 Action
 *@author： cgn
 *@createdate： 2012-7-7
 */
public class CreditAction extends SessionAction {

	private static final long serialVersionUID = 1L;
	private List<Credit> creditList;
	private Credit credit;
	@Autowired
	private CreditBiz creditBiz;
	
	/**
	 * 保存积分等级
	 * @return 
	 * @throws Exception
	 */
	public String save()throws Exception{
			try {
				creditBiz.save(credit);
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
				e.printStackTrace();
				return ERROR;
			}
			return SUCCESS;
		
	}
	/**
	 * 更新会员等级
	 * @return
	 */
	public String update()throws Exception{
		try {
			creditBiz.update(credit);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 删除会员等级
	 * @return 
	 * @throws Exception
	 */
	public String delete() throws Exception{
		try {
			creditBiz.delete(credit.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 分页查询会员等级
	 * @return
	 */
	public String listByPage(){
		pager.setRecordCount(creditBiz.count());
		creditList = creditBiz.listByPage((pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		return SUCCESS;
	}
	
	/**
	 * 根据id查找会员等级
	 * @return
	 */
	public String findById(){
			try {
				credit=creditBiz.get(credit.getId());
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
				e.printStackTrace();
				return ERROR;
			}
			return SUCCESS;
	}
	
	/**
	 * 获取所有会员级别，返回Json数据
	 * @return
	 */
	public String findAllCredit() {
		String json = null;
		try {
			creditList = creditBiz.list();
			json = JsonUtil.toJsonByArray(creditList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ajax(json);
	}
	
	public List<Credit> getCreditList() {
		return creditList;
	}
	public void setCreditList(List<Credit> creditList) {
		this.creditList = creditList;
	}
	public Credit getCredit() {
		return credit;
	}
	public void setCredit(Credit credit) {
		this.credit = credit;
	}
	public void setCreditBiz(CreditBiz creditBiz) {
		this.creditBiz = creditBiz;
	}
	

}
